<?php
    namespace Wulian\Lib;


    /**
     * 电信接口
     */
    class telecom
    {
        public $api_url = "http://api.ct10649.com:9001/m2m_ec/query.do?";
        public $user_id = "shyj11";
        public $passWord = "shyj111A";
        public $key1 = "aWE";
        public $key2 = "gab";
        public $key3 = "o57";
        public $poolNbr = "";   //流量池号码


        /**
         * 公共参数组合
         */
        public function commonParame($method,$param = array()){
            $DesUtils = new \Wulian\Lib\DesUtils();
            $passWord = $DesUtils->strEnc($this->passWord,$this->key1,$this->key2,$this->key3);
            $arr=array($this->user_id,$this->passWord,$method);//加密数组，数组所需参数根据对应的接口文档
            if(!empty($param)){
                foreach($param AS $pindex => $pitem){
                    if($pindex != 'needDtl' && $pindex != "startDate" && $pindex != 'endDate'){
                        $arr[] = $pitem;
                    }

                }

            }
            $arrOrder=$DesUtils->naturalOrdering($arr); //加密数组元素排序
            $sign = $DesUtils->strEnc($arrOrder,$this->key1,$this->key2,$this->key3);

            $data = array(
                "method"=>$method,
                "user_id"=>$this->user_id,
                "passWord"=>$passWord,
                "sign"=>$sign
            );
            $data = array_merge($data,$param);
            //echo "<pre>";print_r($data);
            return $data;
        }


        /**
         * 流量池列表查询接口
         */
        public function getPoolList(){
            $method = "getPoolList";
            $parame = $this->commonParame($method);
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 流量池使用量
         */
        public function getPoolQry(){
            $method = "poolQry";
            $parame = $this->commonParame($method,array("poolNbr"=>$this->poolNbr));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 流量池单个成员使用量查询接口
         * monthSelect  1当月 0上月
         * member_accNbr 流量池成员号码
         */
        public function getPoolMemQry($monthSelect,$member_accNbr){
            $method = "poolMemQry";
            $parame = $this->commonParame($method,array("poolNbr"=>$this->poolNbr,"monthSelect"=>$monthSelect,"member_accNbr"=>$member_accNbr));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 卡状态查询
         */
        public function getQueryCardStatus($access_number){
            $method = "queryCardStatus";
            $parame = $this->commonParame($method,array("access_number"=>$access_number));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 卡成员查询
         */
        public function getPoolMember($member_accNbr){
            $method = "getPoolMember";
            $parame = $this->commonParame($method,array("poolNbr"=>$this->poolNbr,"member_accNbr"=>$member_accNbr));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 活卡激活
         */
        public function requestServActive($access_number){
            $method = "requestServActive";
            $parame = $this->commonParame($method,array("access_number"=>$access_number));
            $result = $this->getJsonResult($parame,$method);
            return $result;
        }

        /**
         * 停机保号
         *  $orderTypeId  19表示停机保号，20表示停机保号复机
         */
        public function disabledNumber($access_number,$orderTypeId){
            $method = "disabledNumber";
            $parame = $this->commonParame($method,array("access_number"=>$access_number,"acctCd"=>"","orderTypeId"=>$orderTypeId));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 修改成员流量池限额
         */
        public function modifyPoolMember($member_accNbr,$flow_quota){
            $method = "modifyPoolMember";
            $parame = $this->commonParame($method,array("poolNbr"=>$this->poolNbr,"member_accNbr"=>$member_accNbr,"flow_quota"=>$flow_quota));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 当月已使用流量查询
         */
        public function queryTraffic($access_number = "",$needDtl=1){
            $method = "queryTraffic";
            $parame = $this->commonParame($method,array("access_number"=>$access_number,"needDtl"=>$needDtl));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 按制定日期查看流量使用记录
         */
            public function queryTrafficByDate($access_number,$startDate,$endDate,$needDtl = 1){
            $method = "queryTrafficByDate";
            $parame = $this->commonParame($method,array("access_number"=>$access_number,"startDate"=>$startDate,"endDate"=>$endDate,"needDtl"=>$needDtl));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 流量池充值
         */
        public function flowPool($charge){
            $method = "flowPoolPay";
            $parame = $this->commonParame($method,array("poolNbr"=>$this->poolNbr,"charge"=>$charge));
            $result = $this->getResult($parame,$method);
            return $result;
        }

        /**
         * 解析xml
         */
        public function xmlToArray($xml_data){
            $xml_data = (array)$xml_data;
            foreach($xml_data AS &$xitem){
               if(is_object($xitem)){
                   $xitem = $this->xmlToArray($xitem);
               }
            }
            return $xml_data;
        }

        /**
         * @param $data
         * 调取接口 返回数据结果
         */
        public function getResult($data,$api_log_method = ''){
            $start_time = time();
            $data = http_build_query($data);
            $result = file_get_contents($this->api_url.$data);
            $result_real = $result;
            $result = str_replace('<web:NEW_DATA_TICKET_QRsp xmlns:web="http://www.example.org/webservice">','',$result);
            $result = str_replace('</web:NEW_DATA_TICKET_QRsp>','',$result);
            $result = simplexml_load_string($result);
            $result = $this->xmlToArray($result);
            $cost_time = time() - $start_time;
            $this->addApiLog($this->api_url.$data,$api_log_method,trim($result)?json_encode($result, JSON_UNESCAPED_UNICODE):$result_real,$cost_time);
            return $result;
        }

        /**
         * JSON返回
         */
        public function getJsonResult($data,$api_log_method = ''){
            $start_time = time();
            $data = http_build_query($data);
            $result = file_get_contents($this->api_url.$data);
            $result = json_decode($result,true);
            $cost_time = time() - $start_time;
            $this->addApiLog($this->api_url.$data,$api_log_method,json_encode($result, JSON_UNESCAPED_UNICODE),$cost_time);
            return $result;
        }

        /**
         * 接口返回日志
         */
        public function addApiLog($api_log_url,$api_log_method,$api_log_result,$api_log_cost_time = 0){
            $insert = array(
                "api_log_method"=>$api_log_method,
                "api_log_url"=>$api_log_url,
                "api_log_result"=>$api_log_result,
                "api_log_ip"=>get_client_ip(),
                "api_log_cost_time"=>$api_log_cost_time,
                "api_log_uid"=>session("ADMIN_ID")?session("ADMIN_ID"):0,
            );
            M("WlApiLog")->add($insert);
        }



    }
?>